package com.ithema;

import java.util.ArrayList;
import java.util.Scanner;

public class Test5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //定义一个Arraylist集合.用来管理所有数据
        ArrayList<Student> list = new ArrayList<>();
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");

            System.out.println("请输入您的选择:");
            String choise = sc.next();
            switch (choise) {
                case "1":
                    addStudent(list);
                    // System.out.println("添加学生");
                    break;
                case "2":
                    deleteStudent(list);
                    //System.out.println("删除学生");
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updatestudent(list);
                    break;
                case "4":
                    // System.out.println("查看学生");
                    queryStudents(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输出有误");
                    break;
            }

        }
    }

    private static void updatestudent(ArrayList<Student> list) {
        //给出提示(请输入您要删除的学号)
        System.out.println("请输入您要修改的学生学号:");
        Scanner sc = new Scanner(System.in);
        String updateSid = sc.next();
        //用getIndex的方法,查找该学号在集合中出现的索引位置
        int index = getIndex(list,updateSid);
        //根据索引判断,学号在集合中是否存在
        if(index == -1){
            //不存在,给出提示
            System.out.println("查无此人,请重新输入:");
        }else{
            //存在,删除
            System.out.println("请输入姓名:");
            String name = sc.next();
            System.out.println("请输入年龄:");
            int age = sc.nextInt();
            System.out.println("请输入生日:");
            String birthday = sc.next();
            Student stu = new Student(updateSid,name,age,birthday);
            list.set(index,stu);
            System.out.println("修改成功!");
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        //给出提示(请输入您要删除的学号)
        System.out.println("请输入您要删除的学生学号:");
        Scanner sc = new Scanner(System.in);
        String deleteSid = sc.next();
        //用getIndex的方法,查找该学号在集合中出现的索引位置
        int index = getIndex(list,deleteSid);
        //根据索引判断,学号在集合中是否存在
        if(index == -1){
            //不存在,给出提示
            System.out.println("查无此人,请重新输入:");
        }else{
            //存在,删除
            list.remove(index);
            System.out.println("删除成功");
        }
    }


    private static void queryStudents(ArrayList<Student> list) {
        //判断集合中是否存在数据.如果不存在直接给出提示
        if (list.size() == 0) {
            System.out.println("无信息,请添加后重新查询:");
            return;
        }
        //存在:展头表示数据
        System.out.println("学号\t\t姓名\t年龄\t生日");
        //遍历集合,获取每一个学生对象的信息,打印在控制台
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t"+stu.getBirthday());


        }
    }


    public static void addStudent(ArrayList<Student> list) {
        //键盘录入学生的信息
        Scanner sc = new Scanner(System.in);
        String sid;
            System.out.println("请输入学号:");
        while(true) {
            sid = sc.next();
            int index = getIndex(list, sid);
            if (index == -1) {
                break;
                //学号不存在,说明本次录入可用,
            } else {
                System.out.println("学号有误,请重新输入:");
            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();

        //将录入的信息封装成对象
        Student stu = new Student(sid, name, age, birthday);
        //将封装好的信息添加到list容器中
        list.add(stu);
        //给出添加成功的提示信息
        System.out.println("添加成功!:");

    }

    public static int getIndex(ArrayList<Student> list, String sid) {
        //假设传入的学号,在集合中不存在
        int index = -1;

//遍历集合,获取每个学生对象,准备进行查找
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            //获取每一个学生对象的学号
            String id = stu.getSid();
            //获取每个学生对象的学号,和传入的学号进行比对
            if (id.equals(sid)) {
                index = i;

            }
        }
        return index;
    }
}